package com.qhit.config.shiro.realm;

import com.qhit.pojo.Permission;
import com.qhit.pojo.Role;
import com.qhit.pojo.User;
import com.qhit.service.PermissionService;
import com.qhit.service.RoleService;
import com.qhit.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

public class MyRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    //给用户赋予角色和权限
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取用户名
        String username = (String) principalCollection.getPrimaryPrincipal();
        //通过用户名获取用户
        User u = userService.getUserByUserName(username);
        //定义装载角色和用户的集合
        HashSet<String> roles=new HashSet<>();
        HashSet<String> pers=new HashSet<>();

        //通过用户ID获取角色
        List<Role> rlist=roleService.getRolesByUserId(u.getId());
        for (int i = 0; i <rlist.size() ; i++) {
            //将角色名封装到角色的集合里
            roles.add(rlist.get(i).getRoleName());
            //通过角色ID获取角色下的权限

            List<Permission> plist=permissionService.getPermissionByRoleId(rlist.get(i).getId());
            for (int j = 0; j <plist.size() ; j++) {
                pers.add(plist.get(j).getPermissionName())

                ;
            }
        }



        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setRoles(roles);
        simpleAuthorizationInfo.setStringPermissions(pers);
        return simpleAuthorizationInfo;



    }

    public static void main(String[] args) {
        String s = UUID.randomUUID().toString();
        System.out.println(s);
        //1.加密名字
        //2.加密的内容--明文
        //3.盐值
        //4.加密迭代次数0
        SimpleHash simpleHash = new SimpleHash("MD5","123456","1152a29b-75b3-4566-8807-208d8753e215",1024);
        System.out.println(simpleHash.toString());
    }
    //再认证的时候会调用这个方法，（根据authenticationToken里卖提供的信息
    // ，然后封装到AuthenticationInfo传递给SecurityManager）
    //SecurityManager会自动对比密码和用户名是否一致



    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //判断从客户那里输入的用户名是否为空  为空就返回null
        if (authenticationToken.getPrincipal() == null || "".equals(authenticationToken.getPrincipal())) {

            return null;
        }
        //获取用户名
        String username = (String) authenticationToken.getPrincipal();
        //根据用户名去寻找用户
        User u = userService.getUserByUserName(username);
        System.out.println(u.toString());
        if (u == null) {
            return null;
        }

        ByteSource bytes = ByteSource.Util.bytes(u.getSalt());
        SimpleAuthenticationInfo simpleAuthenticationInfo =
                new SimpleAuthenticationInfo(
                        u.getName(),u.getPwd(),bytes,getName()
                );
        return simpleAuthenticationInfo;
    }

}
